package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.MemberDao;
import com.itheima.dto.PageResult;
import com.itheima.dto.QueryPageBean;
import com.itheima.pojo.Member;
import com.itheima.service.MemberService;
import com.itheima.utils.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;

import java.time.LocalDateTime;
import java.util.*;

/**
 * 会员服务
 */
@Service
@Transactional
public class MemberServiceImpl implements MemberService {
    @Autowired
    private MemberDao memberDao;


    public Member findByTelephone(String telephone) {
        return null;
    }

    //新增成员
    public void add(Member member) {
        if(member != null){
            //请求数据没有密码,设置默认密码123456,并用md5加密
            member.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
        }
        //获取当前系统时间,并传入member中
        //LocalDateTime now = LocalDateTime.now();
        Date date = new Date();
        member.setRegTime(date);
        memberDao.add(member);
    }

        //根据id查询会员信息
        @Override
        public Member findById (Integer id){
            Member member = memberDao.findById(id);
            return member;
        }
    //修改会员信息
    @Override
    public void updateById(Member member) {
        memberDao.updateById(member);
    }





        //分页查询
    /*Integer currentPage;页码
    Integer pageSize;每页记录数
    String queryString;查询条件*/
        @Override
        public PageResult findPage (QueryPageBean queryPageBean){
            String queryString = queryPageBean.getQueryString();
            Integer pageSize = queryPageBean.getPageSize();
            Integer currentPage = queryPageBean.getCurrentPage();
            PageHelper.startPage(currentPage, pageSize);

            Page<Member> members = memberDao.queryPage(queryString);
            for (Member member : members) {
                Date birthday = member.getBirthday();
                if (birthday != null) {
                    Date now = new Date();
                    long time = now.getTime() - birthday.getTime();
                    long ageLong = time / (365 * 24 * 60 * 60 * 1000l);
                    int age = (int) ageLong;
                    member.setAge(age);
                }
            }
            return new PageResult(members.getTotal(), members.getResult());
        }

       //根据id删除
        @Override
        public void deleteById (Integer id){
            memberDao.deleteById(id);
        }
    //导入excel
    public List<String> findNames(Integer id) {
        List<Map<String, Object>> list = memberDao.findNames(id);//所有的检查项信息 （套餐 检查组 检查项）
        HashSet<String> setmealSet = new HashSet<>();
        HashSet<String> checkgrouplSet = new HashSet<>();
        HashSet<String> checkitemsSet = new HashSet<>();
        //遍历数据 并获取对应数据
        for (Map<String, Object> map : list) {
            String setmeal = (String) map.get("setmeal");
            String checkgroup = (String) map.get("checkgroups");
            String checkitems = (String) map.get("checkitems");
            setmealSet.add(setmeal);
            checkgrouplSet.add(checkgroup);
            checkitemsSet.add(checkitems);
        }
        String setmealSetStr = setmealSet.toString();
        String checkgrouplSetStr = checkgrouplSet.toString();
        String checkitemsSetStr = checkitemsSet.toString();
        //添加数据
        List<String> nameList = new ArrayList<>();
        nameList.add(setmealSetStr);
        nameList.add(checkgrouplSetStr);
        nameList.add(checkitemsSetStr);
        return nameList;

    }
}